/*
 * analogtv.cpp - analog tv interface
 *
 * Copyright (C) 2010-2011 Serge Begger
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "analogtv.h"
#include "ui_analogtv.h"

analogtv::analogtv(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::analogtv)
{
    ui->setupUi(this);
    initChList();

    currSignal = 0;
    settings = new QSettings("carpc", "analogtv", this);
    settings->setPath(QSettings::IniFormat,QSettings::UserScope,"carpc");
    currVolume = settings->value("currVolume").toInt();
    currCh = settings->value("currCh").toInt();
    if(currCh == 0) currCh = 1;
    currPreset = settings->value("currPreset").toInt();

    presetList = new QList<int>;
    loadPreset();

    chPanel = new numPanel(this,true,QRect(0,90,300,400));
    chPanel->helpLabel->setText("CH: 1 - 12 | 21 - 69");
    chPanel->maxSymb = 2;
    connect(chPanel, SIGNAL(putFreq()), this, SLOT(updateUiCh()));
    connect(chPanel, SIGNAL(endPutFreq()), this, SLOT(setManCh()));

    scanTimer = new QTimer(this);
    connect(scanTimer, SIGNAL(timeout()), this, SLOT(scanCh()));

    gstEngine = new gstengine(this);
}

analogtv::~analogtv()
{
    settings->setValue("currCh", currCh);
    settings->setValue("currVolume", currVolume);
    settings->setValue("currPreset", currCh);
    savePreset();
    delete ui;
}

void analogtv::setVolume(int vol) {
    currVolume = vol;
    gstEngine->setVolume(vol);
    settings->setValue("currVolume"  , currVolume);
}

bool analogtv::init()
{
    if (!gstEngine->analogTv(ui->frameVideo)) {
        delete ui;
        return false;
    }
    gstEngine->setGeometryWV(QRect(0,0,796,491));
    gstEngine->setVideoLCDtext(QString::number(currPreset));
    setChannel();

    if (!gstEngine->loopaudio())
            return false;

    setVolume(currVolume);
    return true;
}

void analogtv::on_backButton_pressed()
{
    emit hideWidget();
}

void analogtv::on_closeButton_pressed()
{
    this->~analogtv();
}

void analogtv::updateUiCh()
{
    ui->lcdCh->display(chPanel->freq->toInt());
}

void analogtv::setManCh()
{
    int tempCh =  chPanel->freq->toInt();
    if (tempCh == 0) return;
    bool chGood =true;
    if(tempCh > 12 && tempCh < 21)
        chGood = false;
    if(tempCh > 69)
        chGood = false;
    if (currCh != tempCh && chGood) {
        currCh = tempCh;
        setChannel();
    } else {
        ui->lcdCh->display(currCh);
    }
}

void analogtv::on_chButton_pressed()
{
    if (!chPanel->isVisible())
        chPanel->onShow();
    else
        chPanel->offShow();
}

void analogtv::on_findButton_pressed()        
{
    if (scanTimer->isActive()) {
        scanTimer->stop();
    } else {
        presetList->clear();
        currCh = 0;
        scanTimer->start(300);
    }
}

void analogtv::on_prevButton_pressed()
{
    if (presetList->count() != 0) {
        currPreset --;
        if (currPreset <= 0) currPreset = 1;
        setPresetCh();
    }
}

void analogtv::on_nextButton_pressed()
{
    if (presetList->count() != 0) {
        currPreset ++;
        if (currPreset > presetList->count()) currPreset = 1;
        setPresetCh();
    }
}

void analogtv::setPresetCh()
{
    currCh = presetList->at(currPreset-1);
    gstEngine->setVideoLCDtext(QString::number(currPreset));
    setChannel();
}

void analogtv::scanCh()
{
    currCh ++;
    if(currCh > 12 && currCh < 21) currCh = 21;
    if(currCh > 69) {
        if(presetList->count() ==  0) {
            currCh = 1;
            setChannel();
        } else {
            currPreset = 1;
            setPresetCh();
        }
        scanTimer->stop();
        return;
    }
    this->repaint(ui->lcdCh->geometry());
    setChannel();
    if (currSignal > 48000 ) {
        presetList->insert(presetList->count()+1,currCh);
        gstEngine->setVideoLCDtext(QString::number(presetList->count()));
    }
}

void analogtv::setChannel()
{
    ui->lcdCh->display(currCh);
    int tempCh = currCh-1;
    if (tempCh > 12)
        tempCh = tempCh - 8;
    double freq = QString(chlist.at(tempCh)).toDouble()*1000;
    gstEngine->setanalogTvCh(freq);
}

void analogtv::loadPreset()
{
    int size = settings->beginReadArray("presetList");
    for (int i=0; i < size; i++) {
        settings->setArrayIndex(i);
        presetList->insert(i, settings->value("ch").toInt());
    }
    settings->endArray();
}

void analogtv::savePreset()
{
    settings->beginWriteArray("presetList");
    for (int i=0; i < presetList->size(); i++) {
        settings->setArrayIndex(i);
        settings->setValue("ch", QString::number(presetList->at(i)));
    }
    settings->endArray();
}

void analogtv::initChList()
{
    //-------RUSSIA
    chlist<<"49750";//1
    chlist<<"59250";
    chlist<<"77250";
    chlist<<"85250";
    chlist<<"93250";
    chlist<<"175250";
    chlist<<"183250";
    chlist<<"191250";
    chlist<<"199250";
    chlist<<"207250";
    chlist<<"215250";
    chlist<<"223250";//12
    //----------UHF
    chlist<<"471250";//21
    chlist<<"479250";
    chlist<<"487250";
    chlist<<"495250";
    chlist<<"503250";
    chlist<<"511250";
    chlist<<"519250";
    chlist<<"527250";
    chlist<<"535250";
    chlist<<"543250";//30
    chlist<<"551250";
    chlist<<"559250";
    chlist<<"567250";
    chlist<<"575250";
    chlist<<"583250";
    chlist<<"591250";
    chlist<<"599250";
    chlist<<"607250";
    chlist<<"615250";
    chlist<<"623250";//40
    chlist<<"631250";
    chlist<<"639250";
    chlist<<"647250";
    chlist<<"655250";
    chlist<<"663250";
    chlist<<"671250";
    chlist<<"679250";
    chlist<<"687250";
    chlist<<"695250";
    chlist<<"703250";//50
    chlist<<"711250";
    chlist<<"719250";
    chlist<<"727250";
    chlist<<"735250";
    chlist<<"743250";
    chlist<<"751250";
    chlist<<"759250";
    chlist<<"767250";
    chlist<<"775250";
    chlist<<"783250";//60
    chlist<<"791250";
    chlist<<"799250";
    chlist<<"807250";
    chlist<<"815250";
    chlist<<"523250";
    chlist<<"831250";
    chlist<<"839250";
    chlist<<"847250";
    chlist<<"855250";
}

